import ajaxAsync from 'vue-xiaobu-utils'
import StationDetailsModel from '../models/StationDetailsModel'
import BusesInfoModel from '../models/BusesInfoModel'
import { getColletctedRouteList } from '../../../utils/CollectionUtils'
/**
 * 查询公交路线、车站
 * @param {Number}  pageNo 页码
 * @param {Number} pageSize 每页条数
 * @param {String} busId 公交站id
 */
async function getStationDetailsInfo(pageNo, pageSize, busId) {
  let sendObj = {
    IMPORT: busId,
    PAGE_CAPACITY: pageSize,
    PAGE_INDEX: pageNo
  }
  let res = await ajaxAsync('/bus/busWithDirectionBystation', sendObj)

  // 获取收藏线路
  let collectedLineArr = await getColletctedRouteList()
  let collectedLineIdArr = []
  for (let i in collectedLineArr) {
    collectedLineIdArr.push(
      collectedLineArr[i].SUBROUTE_ID + '' + collectedLineArr[i].TYPE
    )
  }

  let busesInfoList = []
  if (res.LIST && res.COUNT > 0) {
    res.LIST.map((item, index) => {
      let busesInfoItem = {}
      // 上行
      if (item.UPBUSESINFO) {
        busesInfoItem.upUniqueStationId = item.UP_UNIQUE_STATIONID
        busesInfoItem.upBusesInfoModel = disposeItemRoute(
          item.UPBUSESINFO,
          collectedLineIdArr
        )
      }
      // 下行
      if (item.DOWNBUSESINFO) {
        busesInfoItem.downUniqueStationId = item.UP_UNIQUE_STATIONID
        busesInfoItem.downBusesInfoModel = disposeItemRoute(
          item.DOWNBUSESINFO,
          collectedLineIdArr
        )
      }
      busesInfoList.push(busesInfoItem)
    })
  }
  // console.log(busesInfoList, 'busesInfoList~~~')
  let stationDetailsModel = new StationDetailsModel(res.COUNT, busesInfoList)
  return stationDetailsModel
}

/**
 * 处理上行或下行数据
 * @param {Object}  itemRoute 上行或下行
 * @param {Array}  collectedLineIdArr 收藏线路数组
 */
function disposeItemRoute(itemRoute, collectedLineIdArr) {
  let collectId, isCollect, busesInfoModel, busName
  collectId = itemRoute.SUB_ROUTEID + '' + itemRoute.TYPE
  isCollect = collectedLineIdArr.includes(collectId)
  busName = itemRoute.BUS_NAME
  busName = !/[\u4e00-\u9fa5]+/.test(busName) ? busName + '路' : busName
  busesInfoModel = new BusesInfoModel(
    busName,
    itemRoute.BUS_START_STATION,
    itemRoute.BUS_END_STATION,
    itemRoute.NOWBUS_STOP,
    itemRoute.SUB_ROUTEID,
    itemRoute.PRICE,
    itemRoute.TYPE,
    itemRoute.STARTTIME,
    itemRoute.ENDTIME,
    isCollect
  )
  return busesInfoModel
}

export { getStationDetailsInfo }
